package com.kexio.user.dto;

import java.io.Serializable;

/**
 * 权限统计信息DTO
 * 
 * 用于传输权限相关的统计数据，包括：
 * - 权限总数和状态分布
 * - 权限类型分布
 * - 权限层级分布
 * 
 * @author Kexio Team
 * @since 1.0.0
 */
public class PermissionStatistics implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    // ==================== 基础统计 ====================
    
    /**
     * 权限总数
     */
    private Integer totalPermissions = 0;
    
    /**
     * 启用状态的权限数量
     */
    private Integer enabledPermissions = 0;
    
    /**
     * 禁用状态的权限数量
     */
    private Integer disabledPermissions = 0;
    
    // ==================== 类型统计 ====================
    
    /**
     * 操作权限数量
     */
    private Integer operationPermissions = 0;
    
    /**
     * 资源权限数量
     */
    private Integer resourcePermissions = 0;
    
    /**
     * API权限数量
     */
    private Integer apiPermissions = 0;
    
    // ==================== 层级统计 ====================
    
    /**
     * 根权限数量（没有父权限的权限）
     */
    private Integer rootPermissions = 0;
    
    /**
     * 子权限数量（有父权限的权限）
     */
    private Integer childPermissions = 0;
    
    /**
     * 最大权限层级深度
     */
    private Integer maxDepth = 0;
    
    // ==================== 关联统计 ====================
    
    /**
     * 已分配给角色的权限数量
     */
    private Integer assignedToRoles = 0;
    
    /**
     * 未分配给任何角色的权限数量
     */
    private Integer unassignedPermissions = 0;
    
    /**
     * 关联的角色总数
     */
    private Integer relatedRolesCount = 0;
    
    /**
     * 关联的用户总数（通过角色间接关联）
     */
    private Integer relatedUsersCount = 0;
    
    // ==================== 构造函数 ====================
    
    public PermissionStatistics() {
    }
    
    // ==================== Getter/Setter ====================
    
    public Integer getTotalPermissions() {
        return totalPermissions;
    }
    
    public void setTotalPermissions(Integer totalPermissions) {
        this.totalPermissions = totalPermissions;
    }
    
    public Integer getEnabledPermissions() {
        return enabledPermissions;
    }
    
    public void setEnabledPermissions(Integer enabledPermissions) {
        this.enabledPermissions = enabledPermissions;
    }
    
    public Integer getDisabledPermissions() {
        return disabledPermissions;
    }
    
    public void setDisabledPermissions(Integer disabledPermissions) {
        this.disabledPermissions = disabledPermissions;
    }
    
    public Integer getOperationPermissions() {
        return operationPermissions;
    }
    
    public void setOperationPermissions(Integer operationPermissions) {
        this.operationPermissions = operationPermissions;
    }
    
    public Integer getResourcePermissions() {
        return resourcePermissions;
    }
    
    public void setResourcePermissions(Integer resourcePermissions) {
        this.resourcePermissions = resourcePermissions;
    }
    
    public Integer getApiPermissions() {
        return apiPermissions;
    }
    
    public void setApiPermissions(Integer apiPermissions) {
        this.apiPermissions = apiPermissions;
    }
    
    public Integer getRootPermissions() {
        return rootPermissions;
    }
    
    public void setRootPermissions(Integer rootPermissions) {
        this.rootPermissions = rootPermissions;
    }
    
    public Integer getChildPermissions() {
        return childPermissions;
    }
    
    public void setChildPermissions(Integer childPermissions) {
        this.childPermissions = childPermissions;
    }
    
    public Integer getMaxDepth() {
        return maxDepth;
    }
    
    public void setMaxDepth(Integer maxDepth) {
        this.maxDepth = maxDepth;
    }
    
    public Integer getAssignedToRoles() {
        return assignedToRoles;
    }
    
    public void setAssignedToRoles(Integer assignedToRoles) {
        this.assignedToRoles = assignedToRoles;
    }
    
    public Integer getUnassignedPermissions() {
        return unassignedPermissions;
    }
    
    public void setUnassignedPermissions(Integer unassignedPermissions) {
        this.unassignedPermissions = unassignedPermissions;
    }
    
    public Integer getRelatedRolesCount() {
        return relatedRolesCount;
    }
    
    public void setRelatedRolesCount(Integer relatedRolesCount) {
        this.relatedRolesCount = relatedRolesCount;
    }
    
    public Integer getRelatedUsersCount() {
        return relatedUsersCount;
    }
    
    public void setRelatedUsersCount(Integer relatedUsersCount) {
        this.relatedUsersCount = relatedUsersCount;
    }
    
    // ==================== 便捷方法 ====================
    
    /**
     * 计算启用权限的百分比
     * 
     * @return 启用权限百分比（0-100）
     */
    public double getEnabledPercentage() {
        if (totalPermissions == null || totalPermissions == 0) {
            return 0.0;
        }
        return (enabledPermissions * 100.0) / totalPermissions;
    }
    
    /**
     * 计算禁用权限的百分比
     * 
     * @return 禁用权限百分比（0-100）
     */
    public double getDisabledPercentage() {
        if (totalPermissions == null || totalPermissions == 0) {
            return 0.0;
        }
        return (disabledPermissions * 100.0) / totalPermissions;
    }
    
    /**
     * 计算已分配权限的百分比
     * 
     * @return 已分配权限百分比（0-100）
     */
    public double getAssignedPercentage() {
        if (totalPermissions == null || totalPermissions == 0) {
            return 0.0;
        }
        return (assignedToRoles * 100.0) / totalPermissions;
    }
    
    /**
     * 验证统计数据的一致性
     * 
     * @return true-数据一致，false-数据不一致
     */
    public boolean isDataConsistent() {
        // 检查状态统计一致性
        if (enabledPermissions + disabledPermissions != totalPermissions) {
            return false;
        }
        
        // 检查类型统计一致性
        if (operationPermissions + resourcePermissions + apiPermissions > totalPermissions) {
            return false;
        }
        
        // 检查层级统计一致性
        if (rootPermissions + childPermissions != totalPermissions) {
            return false;
        }
        
        // 检查分配统计一致性
        if (assignedToRoles + unassignedPermissions > totalPermissions) {
            return false;
        }
        
        return true;
    }
    
    /**
     * 重新计算衍生统计数据
     */
    public void recalculate() {
        if (totalPermissions != null && enabledPermissions != null) {
            this.disabledPermissions = totalPermissions - enabledPermissions;
        }
        
        if (totalPermissions != null && rootPermissions != null) {
            this.childPermissions = totalPermissions - rootPermissions;
        }
        
        if (totalPermissions != null && assignedToRoles != null) {
            this.unassignedPermissions = totalPermissions - assignedToRoles;
        }
    }
    
    @Override
    public String toString() {
        return "PermissionStatistics{" +
                "totalPermissions=" + totalPermissions +
                ", enabledPermissions=" + enabledPermissions +
                ", disabledPermissions=" + disabledPermissions +
                ", operationPermissions=" + operationPermissions +
                ", resourcePermissions=" + resourcePermissions +
                ", apiPermissions=" + apiPermissions +
                ", rootPermissions=" + rootPermissions +
                ", childPermissions=" + childPermissions +
                ", maxDepth=" + maxDepth +
                ", assignedToRoles=" + assignedToRoles +
                ", unassignedPermissions=" + unassignedPermissions +
                ", relatedRolesCount=" + relatedRolesCount +
                ", relatedUsersCount=" + relatedUsersCount +
                '}';
    }
}
